{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "      <th>x3</th>\n",
       "      <th>x4</th>\n",
       "      <th>x5</th>\n",
       "      <th>x6</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1999</th>\n",
       "      <td>93.18</td>\n",
       "      <td>21391758</td>\n",
       "      <td>7980207</td>\n",
       "      <td>6661555</td>\n",
       "      <td>0.373051</td>\n",
       "      <td>0</td>\n",
       "      <td>288972</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000</th>\n",
       "      <td>115.60</td>\n",
       "      <td>24927434</td>\n",
       "      <td>8779835</td>\n",
       "      <td>7839516</td>\n",
       "      <td>0.352216</td>\n",
       "      <td>0</td>\n",
       "      <td>350495</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001</th>\n",
       "      <td>114.13</td>\n",
       "      <td>28416511</td>\n",
       "      <td>9554676</td>\n",
       "      <td>8803979</td>\n",
       "      <td>0.336237</td>\n",
       "      <td>0</td>\n",
       "      <td>443213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002</th>\n",
       "      <td>141.49</td>\n",
       "      <td>32039616</td>\n",
       "      <td>10509450</td>\n",
       "      <td>9733195</td>\n",
       "      <td>0.328014</td>\n",
       "      <td>0</td>\n",
       "      <td>526377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003</th>\n",
       "      <td>180.52</td>\n",
       "      <td>37586166</td>\n",
       "      <td>13141254</td>\n",
       "      <td>11833760</td>\n",
       "      <td>0.349630</td>\n",
       "      <td>0</td>\n",
       "      <td>581898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004</th>\n",
       "      <td>233.14</td>\n",
       "      <td>44505503</td>\n",
       "      <td>15941538</td>\n",
       "      <td>14030973</td>\n",
       "      <td>0.358193</td>\n",
       "      <td>4636933</td>\n",
       "      <td>528365</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005</th>\n",
       "      <td>268.07</td>\n",
       "      <td>51542283</td>\n",
       "      <td>18439550</td>\n",
       "      <td>16171817</td>\n",
       "      <td>0.357756</td>\n",
       "      <td>5574275</td>\n",
       "      <td>816119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006</th>\n",
       "      <td>313.85</td>\n",
       "      <td>60818614</td>\n",
       "      <td>22270093</td>\n",
       "      <td>18921556</td>\n",
       "      <td>0.366172</td>\n",
       "      <td>5907373</td>\n",
       "      <td>967265</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007</th>\n",
       "      <td>355.91</td>\n",
       "      <td>71403223</td>\n",
       "      <td>26029310</td>\n",
       "      <td>22841850</td>\n",
       "      <td>0.364540</td>\n",
       "      <td>6875421</td>\n",
       "      <td>1115007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008</th>\n",
       "      <td>389.47</td>\n",
       "      <td>82873816</td>\n",
       "      <td>29724781</td>\n",
       "      <td>27953721</td>\n",
       "      <td>0.358675</td>\n",
       "      <td>9328615</td>\n",
       "      <td>1287226</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009</th>\n",
       "      <td>392.82</td>\n",
       "      <td>91382135</td>\n",
       "      <td>31173422</td>\n",
       "      <td>31565720</td>\n",
       "      <td>0.341133</td>\n",
       "      <td>11237237</td>\n",
       "      <td>1375085</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010</th>\n",
       "      <td>553.89</td>\n",
       "      <td>107482828</td>\n",
       "      <td>36449611</td>\n",
       "      <td>38835933</td>\n",
       "      <td>0.339120</td>\n",
       "      <td>13541607</td>\n",
       "      <td>1594182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011</th>\n",
       "      <td>596.94</td>\n",
       "      <td>124234390</td>\n",
       "      <td>41405926</td>\n",
       "      <td>45444614</td>\n",
       "      <td>0.333289</td>\n",
       "      <td>15947698</td>\n",
       "      <td>1573830</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012</th>\n",
       "      <td>582.52</td>\n",
       "      <td>135512072</td>\n",
       "      <td>42641557</td>\n",
       "      <td>51685711</td>\n",
       "      <td>0.314670</td>\n",
       "      <td>3837376</td>\n",
       "      <td>1758311</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013</th>\n",
       "      <td>560.89</td>\n",
       "      <td>154201434</td>\n",
       "      <td>47548175</td>\n",
       "      <td>59858717</td>\n",
       "      <td>0.308351</td>\n",
       "      <td>4168317</td>\n",
       "      <td>2216017</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          x1         x2        x3        x4        x5        x6        y\n",
       "1999   93.18   21391758   7980207   6661555  0.373051         0   288972\n",
       "2000  115.60   24927434   8779835   7839516  0.352216         0   350495\n",
       "2001  114.13   28416511   9554676   8803979  0.336237         0   443213\n",
       "2002  141.49   32039616  10509450   9733195  0.328014         0   526377\n",
       "2003  180.52   37586166  13141254  11833760  0.349630         0   581898\n",
       "2004  233.14   44505503  15941538  14030973  0.358193   4636933   528365\n",
       "2005  268.07   51542283  18439550  16171817  0.357756   5574275   816119\n",
       "2006  313.85   60818614  22270093  18921556  0.366172   5907373   967265\n",
       "2007  355.91   71403223  26029310  22841850  0.364540   6875421  1115007\n",
       "2008  389.47   82873816  29724781  27953721  0.358675   9328615  1287226\n",
       "2009  392.82   91382135  31173422  31565720  0.341133  11237237  1375085\n",
       "2010  553.89  107482828  36449611  38835933  0.339120  13541607  1594182\n",
       "2011  596.94  124234390  41405926  45444614  0.333289  15947698  1573830\n",
       "2012  582.52  135512072  42641557  51685711  0.314670   3837376  1758311\n",
       "2013  560.89  154201434  47548175  59858717  0.308351   4168317  2216017"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from GM11 import GM11 # 引入自己编写的灰色预测函数\n",
    "\n",
    "inputfile1 = 'data2.csv'\n",
    "\n",
    "data = pd.read_csv(inputfile1)\n",
    "data.index = range(1999,2014)\n",
    "data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "对于模型x1，该模型精度为---好\n",
      "对于模型x3，该模型精度为---好\n",
      "对于模型x5，该模型精度为---合格\n"
     ]
    }
   ],
   "source": [
    "data.loc[2014] = None\n",
    "data.loc[2015] = None\n",
    "h = ['x1', 'x3', 'x5']\n",
    "for i in h:\n",
    "    gm = GM11(data[i][range(1999, 2014)].as_matrix())\n",
    "    f = gm[0] ##获得灰色预测函数\n",
    "    P = gm[-1] # 获得小残差概率\n",
    "    C = gm[-2] # 获得后验比差值\n",
    "    data[i][2014] = f(len(data)-1)\n",
    "    data[i][2015] = f(len(data))\n",
    "    data[i] = data[i].round(6) # 保留2位小数\n",
    "    if (C < 0.35 and P > 0.95): # 评测后验差判别\n",
    "        print '对于模型%s，该模型精度为---好' % i\n",
    "    elif (C < 0.5 and P > 0.8):\n",
    "        print '对于模型%s，该模型精度为---合格' % i\n",
    "    elif (C < 0.65 and P > 0.7):\n",
    "        print '对于模型%s，该模型精度为---勉强合格' % i\n",
    "    else:\n",
    "        print '对于模型%s，该模型精度为---不合格' % i\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#保存的表名命名格式为“2_2_2_k此表功能名称”，是此小节生成的第1张表格，功能为greyPredict：灰色预测\n",
    "data[h+['y']].to_excel('2_2_2_1greyPredict.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x1</th>\n",
       "      <th>x2</th>\n",
       "      <th>x3</th>\n",
       "      <th>x4</th>\n",
       "      <th>x5</th>\n",
       "      <th>x6</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1999</th>\n",
       "      <td>93.180000</td>\n",
       "      <td>21391758.0</td>\n",
       "      <td>7.980207e+06</td>\n",
       "      <td>6661555.0</td>\n",
       "      <td>0.373051</td>\n",
       "      <td>0.0</td>\n",
       "      <td>288972.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000</th>\n",
       "      <td>115.600000</td>\n",
       "      <td>24927434.0</td>\n",
       "      <td>8.779835e+06</td>\n",
       "      <td>7839516.0</td>\n",
       "      <td>0.352216</td>\n",
       "      <td>0.0</td>\n",
       "      <td>350495.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001</th>\n",
       "      <td>114.130000</td>\n",
       "      <td>28416511.0</td>\n",
       "      <td>9.554676e+06</td>\n",
       "      <td>8803979.0</td>\n",
       "      <td>0.336237</td>\n",
       "      <td>0.0</td>\n",
       "      <td>443213.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002</th>\n",
       "      <td>141.490000</td>\n",
       "      <td>32039616.0</td>\n",
       "      <td>1.050945e+07</td>\n",
       "      <td>9733195.0</td>\n",
       "      <td>0.328014</td>\n",
       "      <td>0.0</td>\n",
       "      <td>526377.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003</th>\n",
       "      <td>180.520000</td>\n",
       "      <td>37586166.0</td>\n",
       "      <td>1.314125e+07</td>\n",
       "      <td>11833760.0</td>\n",
       "      <td>0.349630</td>\n",
       "      <td>0.0</td>\n",
       "      <td>581898.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004</th>\n",
       "      <td>233.140000</td>\n",
       "      <td>44505503.0</td>\n",
       "      <td>1.594154e+07</td>\n",
       "      <td>14030973.0</td>\n",
       "      <td>0.358193</td>\n",
       "      <td>4636933.0</td>\n",
       "      <td>528365.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005</th>\n",
       "      <td>268.070000</td>\n",
       "      <td>51542283.0</td>\n",
       "      <td>1.843955e+07</td>\n",
       "      <td>16171817.0</td>\n",
       "      <td>0.357756</td>\n",
       "      <td>5574275.0</td>\n",
       "      <td>816119.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006</th>\n",
       "      <td>313.850000</td>\n",
       "      <td>60818614.0</td>\n",
       "      <td>2.227009e+07</td>\n",
       "      <td>18921556.0</td>\n",
       "      <td>0.366172</td>\n",
       "      <td>5907373.0</td>\n",
       "      <td>967265.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007</th>\n",
       "      <td>355.910000</td>\n",
       "      <td>71403223.0</td>\n",
       "      <td>2.602931e+07</td>\n",
       "      <td>22841850.0</td>\n",
       "      <td>0.364540</td>\n",
       "      <td>6875421.0</td>\n",
       "      <td>1115007.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008</th>\n",
       "      <td>389.470000</td>\n",
       "      <td>82873816.0</td>\n",
       "      <td>2.972478e+07</td>\n",
       "      <td>27953721.0</td>\n",
       "      <td>0.358675</td>\n",
       "      <td>9328615.0</td>\n",
       "      <td>1287226.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009</th>\n",
       "      <td>392.820000</td>\n",
       "      <td>91382135.0</td>\n",
       "      <td>3.117342e+07</td>\n",
       "      <td>31565720.0</td>\n",
       "      <td>0.341133</td>\n",
       "      <td>11237237.0</td>\n",
       "      <td>1375085.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010</th>\n",
       "      <td>553.890000</td>\n",
       "      <td>107482828.0</td>\n",
       "      <td>3.644961e+07</td>\n",
       "      <td>38835933.0</td>\n",
       "      <td>0.339120</td>\n",
       "      <td>13541607.0</td>\n",
       "      <td>1594182.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011</th>\n",
       "      <td>596.940000</td>\n",
       "      <td>124234390.0</td>\n",
       "      <td>4.140593e+07</td>\n",
       "      <td>45444614.0</td>\n",
       "      <td>0.333289</td>\n",
       "      <td>15947698.0</td>\n",
       "      <td>1573830.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012</th>\n",
       "      <td>582.520000</td>\n",
       "      <td>135512072.0</td>\n",
       "      <td>4.264156e+07</td>\n",
       "      <td>51685711.0</td>\n",
       "      <td>0.314670</td>\n",
       "      <td>3837376.0</td>\n",
       "      <td>1758311.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013</th>\n",
       "      <td>560.890000</td>\n",
       "      <td>154201434.0</td>\n",
       "      <td>4.754818e+07</td>\n",
       "      <td>59858717.0</td>\n",
       "      <td>0.308351</td>\n",
       "      <td>4168317.0</td>\n",
       "      <td>2216017.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014</th>\n",
       "      <td>767.588538</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.816323e+07</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.329010</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015</th>\n",
       "      <td>862.299420</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.580373e+07</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.327145</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              x1           x2            x3          x4        x5          x6  \\\n",
       "1999   93.180000   21391758.0  7.980207e+06   6661555.0  0.373051         0.0   \n",
       "2000  115.600000   24927434.0  8.779835e+06   7839516.0  0.352216         0.0   \n",
       "2001  114.130000   28416511.0  9.554676e+06   8803979.0  0.336237         0.0   \n",
       "2002  141.490000   32039616.0  1.050945e+07   9733195.0  0.328014         0.0   \n",
       "2003  180.520000   37586166.0  1.314125e+07  11833760.0  0.349630         0.0   \n",
       "2004  233.140000   44505503.0  1.594154e+07  14030973.0  0.358193   4636933.0   \n",
       "2005  268.070000   51542283.0  1.843955e+07  16171817.0  0.357756   5574275.0   \n",
       "2006  313.850000   60818614.0  2.227009e+07  18921556.0  0.366172   5907373.0   \n",
       "2007  355.910000   71403223.0  2.602931e+07  22841850.0  0.364540   6875421.0   \n",
       "2008  389.470000   82873816.0  2.972478e+07  27953721.0  0.358675   9328615.0   \n",
       "2009  392.820000   91382135.0  3.117342e+07  31565720.0  0.341133  11237237.0   \n",
       "2010  553.890000  107482828.0  3.644961e+07  38835933.0  0.339120  13541607.0   \n",
       "2011  596.940000  124234390.0  4.140593e+07  45444614.0  0.333289  15947698.0   \n",
       "2012  582.520000  135512072.0  4.264156e+07  51685711.0  0.314670   3837376.0   \n",
       "2013  560.890000  154201434.0  4.754818e+07  59858717.0  0.308351   4168317.0   \n",
       "2014  767.588538          NaN  5.816323e+07         NaN  0.329010         NaN   \n",
       "2015  862.299420          NaN  6.580373e+07         NaN  0.327145         NaN   \n",
       "\n",
       "              y  \n",
       "1999   288972.0  \n",
       "2000   350495.0  \n",
       "2001   443213.0  \n",
       "2002   526377.0  \n",
       "2003   581898.0  \n",
       "2004   528365.0  \n",
       "2005   816119.0  \n",
       "2006   967265.0  \n",
       "2007  1115007.0  \n",
       "2008  1287226.0  \n",
       "2009  1375085.0  \n",
       "2010  1594182.0  \n",
       "2011  1573830.0  \n",
       "2012  1758311.0  \n",
       "2013  2216017.0  \n",
       "2014        NaN  \n",
       "2015        NaN  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
